//
//  MapViewController.m
//  WhereToTravel
//
//  Created by dlios on 15/10/23.
//  Copyright © 2015年 zhoushengmin. All rights reserved.
//

#import "MapViewController.h"
#import "ZSMAFNetWork.h"
#import "MapionModel.h"
#import "MyAnimatedAnnotationView.h"
#import "CityViewController.h"
#define W [[UIScreen mainScreen]bounds].size.width/375
#define H [[UIScreen mainScreen]bounds].size.height /667
@interface MapViewController ()<BMKMapViewDelegate>
{
  BMKPointAnnotation *pointAnnotation;
  BMKPointAnnotation *animatedAnnotation;
}
@property (nonatomic, strong)BMKMapView *mapView;
@property (nonatomic, strong)NSMutableArray *arr;


@end

@implementation MapViewController
- (void)creatData {
    NSString *url = [NSString stringWithFormat:@"http://open.qyer.com/qyer/map/city_list?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=863151025525517&track_app_version=6.7.1&track_app_channel=baidu&track_device_info=mx3&track_os=Android4.4.4&app_installtime=1444613810999&lat=38.883531&lon=121.544905&country_id=%@",self.countryId];
    [ZSMAFNetWork geturl:url body:nil result:ZSMJSON headerFile:nil success:^(id result) {
        NSArray *arr = result[@"data"];
        self.arr = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            MapionModel *mapModel = [[MapionModel alloc]initWithDic:dic];
            [self.arr addObject:mapModel];
        }
        
    } failure:^(NSError *error) {
        
        
    }];
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];

    [self creatData];
   [self addAnimatedAnnotation];
     MapionModel *mapModel = self.arr[0];
    
    CLLocationCoordinate2D coor;
    coor.latitude = [mapModel.lat doubleValue];
    coor.longitude = [mapModel.lng doubleValue];
    
    self.mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, W*375, H*667)];
    self.mapView.mapType = BMKMapTypeStandard;
    self.mapView.zoomLevel = 11;
    [self.view addSubview:self.mapView];

    [self setMapRegionWithCoordinate:CLLocationCoordinate2DMake(coor.latitude, coor.longitude)];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"返回"] style:UIBarButtonItemStylePlain target:nil action:nil];
   
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回"] style:(UIBarButtonItemStylePlain) target:self action:@selector(leftAction:)];

 
    // Do any additional setup after loading the view.
}
- (void)leftAction:(UIBarButtonItem *)barButton {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated {
    [self.mapView viewWillAppear];
    self.mapView.delegate = self;
}
- (void)viewWillDisappear:(BOOL)animated {
    [self.mapView viewWillDisappear];
    self.mapView.delegate = nil;
  
    
}
// 加入经纬度,将mapView锁定到以当前经纬度为中心点的显示区域和合适的显示范围
- (void)setMapRegionWithCoordinate:(CLLocationCoordinate2D)coordinate
{
    BMKCoordinateRegion region;
    
    region = BMKCoordinateRegionMake(coordinate, BMKCoordinateSpanMake(2.5, 2.5));
    // 执行设定显示范围
    [self.mapView setRegion:region animated:YES];
//    // 根据提供的经纬度为中心原点,
//    [self.mapView setCenterCoordinate:coordinate animated:YES];
    
    
}
- (void)addAnimatedAnnotation {
    for (NSInteger i = 0; i < self.arr.count; i++) {
        if (i == 0) {
            MapionModel *mapModel = self.arr[i];
            self.mapView.centerCoordinate = CLLocationCoordinate2DMake([mapModel.lng doubleValue], [mapModel.lat doubleValue]);
        }

             MapionModel *mapModel = self.arr[i];
            animatedAnnotation = [[BMKPointAnnotation alloc]init];
            CLLocationCoordinate2D coor;

            coor.latitude = [mapModel.lat doubleValue];
            coor.longitude = [mapModel.lng doubleValue];
            animatedAnnotation.coordinate = coor;
            NSLog(@"日日日日日日日日日日日%g",coor.latitude);
        
           animatedAnnotation.title = mapModel.cnname;
        animatedAnnotation.subtitle = mapModel.enname;
        
        [self.mapView addAnnotation:animatedAnnotation];
       
    }
    
    


}

//- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation {
//    // 自定义annotation
//    if (annotation == animatedAnnotation) {
//        static NSString *annotationViewID = @"animatedAnnotation";
//        MyAnimatedAnnotationView *annotationView = nil;
//        if (annotationView == nil) {
//            annotationView = [[MyAnimatedAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationViewID];
//            [annotationView setImage:[UIImage imageNamed:@"iconfont-coordinate"]];
//            
//            return annotationView;
//        }
//
//    }
//    return nil;
//    //    NSMutableArray *images = [NSMutableArray array];
////    for (int i = 1; i < 4; i++) {
////        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"poi_%d.png", i]];
////        [images addObject:image];
////    }
////    annotationView.annotationImages = images;
//    
//}
//// 当选中一个annotation views时,调用此接口
- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view {
    
    NSLog(@"点击大头针触发的方法");
    
}
//弹出泡泡点击方法
- (void)mapView:(BMKMapView *)mapView annotationViewForBubble:(BMKAnnotationView *)view {
    CityViewController *cityVC = [[CityViewController alloc]init];
    
   
    for (int i = 0; i < self.arr.count; i++) {
        MapionModel *mapModel = self.arr[i];
        if ([view.annotation.title isEqualToString:mapModel.cnname]) {
            cityVC.cityId = mapModel.uId;
            break;
        }
    }

    [self.navigationController pushViewController:cityVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
